<?php namespace Summer\Hjd\Controllers;

use Backend\Classes\Controller;
use BackendMenu;
use Input;
use Flash;
use ApplicationException;
use RainLab\User\Models\User as UserModel;
use Redirect;
use Illuminate\Support\Carbon;
use Summer\Hjd\Classes\CommonApi;
use Summer\Hjd\Models\WdRecordModel;
use Summer\Hub\Models\PromptModel;

class WdRecord extends Controller
{
    public $implement = [
        'Backend\Behaviors\ListController',
        'Backend.Behaviors.ImportExportController'
    ];

    public $listConfig = 'config_list.yaml';
    public $importExportConfig = 'config_import_export.yaml';

    public $requiredPermissions = [
        'hjd_wd_record' 
    ];

    public function __construct()
    {
        parent::__construct();
        \BackendMenu::setContext('Winter.System', 'main-menu-item-hjd_enduser');
        \Summer\Hub\Classes\SideNavManager::setContext('Summer.Hjd', 'side-menu-item-hjd_wd_record');
    }

    /**
     * 审核
     */
    public function onShenhe(){
        $data=Input::all();
        $record_id=$data['record_id'];
        $remark_admin=$data['remark_admin'];
        $status_sh=$data['status_sh'];
        if($status_sh==1){
            $status=2;//通过
        }else{
            $status=-1;//拒绝
        }
        try {
            $record_model=WdRecordModel::where('id',$record_id)->where('status',1)->first();
            if(!$record_model){
                throw new ApplicationException('记录不存在或不能操作');
            }
            $record_model->remark_admin=$remark_admin;
            $record_model->status=$status;
            $record_model->handled_at=Carbon::now();
            $record_model->save();
            if($status_sh!=1){//拒绝提现，金额返回
                $currency_slug=$record_model->currency_slug;
                $user_id=$record_model->user_id;
                $money=$record_model->money;
                $change_slug='with_back';//拒绝返回
                $table_id=$record_model->id;
                $table_name='summer_hjd_wd_record';
                $commonapi=new CommonApi();
                $commonapi->settleDetailed($user_id,$user_id,$money,1,$currency_slug,[
                    'change_slug'=>$change_slug,
                    'table_id'=>$table_id,
                    'table_name'=>$table_name,
                ]);
            }
            Flash::success('操作成功!');
        }
        catch (ApplicationException $e) {
            Flash::error($e->getMessage());
        }
        return Redirect::refresh();
    }
    public function onListBtnPromptForm(){
        $listBtnParams=Input::all();
        $config_path=$listBtnParams['config_path']??'$/summer/hjd/config/list_prompt_btn_fields.yaml';
        $widgetConfig = $this->makeConfig($config_path);
        $widgetConfig->model=new PromptModel();
        $listBtnPromptFormWidget=$this->makeWidget('Backend\Widgets\Form', $widgetConfig);
        $this->vars['listBtnPromptFormWidget'] = $listBtnPromptFormWidget;
        $this->vars['listBtnParams'] = $listBtnParams;

        return $this->makePartial('$/summer/hub/partials/_list_btn_prompt.htm');
    }

    public function onListBtnInfoForm(){
        $listBtnParams=Input::all();
        $wd_info=WdRecordModel::find($listBtnParams['record_id']);
        $config_path=$listBtnParams['config_path'];
        $widgetConfig = $this->makeConfig($config_path);
        $widgetConfig->model=UserModel::find($wd_info->user_id);
        $listBtnInfoFormWidget=$this->makeWidget('Backend\Widgets\Form', $widgetConfig);
        $this->vars['listBtnInfoFormWidget'] = $listBtnInfoFormWidget;
        $this->vars['listBtnParams'] = $listBtnParams;

        return $this->makePartial('$/summer/hub/partials/_list_btn_info.htm');
    }

}
